package figure8.GUI;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiUnavailableException;
import javax.sound.sampled.Mixer;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.Border;

import figure8.InputMixer;
import figure8.Rec;

public class AudioInputWindow extends JDialog {
	int selectedItem;
	public AudioInputWindow(final MainWindow window, final InputMixer[] inputMixers, final Rec rec) {
		super();

		this.setLocation(window.getFrame().getX() + 80, window.getFrame()
				.getY() + 120);
		this.setIconImage(Toolkit.getDefaultToolkit().getImage("src/figure8/GUI/Fig8-small.png"));
		this.setAlwaysOnTop(true);

		Box column = Box.createVerticalBox();
		
		final AudioInputWindow audioWin = this;
		
		// Add the drop down list with it's elements
		Border rowBorder = BorderFactory.createEmptyBorder(5, 5, 5, 5);
		final JComboBox combo = new JComboBox(inputMixers);
		combo.setBorder(rowBorder);
		
		JButton submit = new JButton("Submit Audio Input");
		JButton submitMIDI = new JButton("MIDI");
		submit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				audioWin.dispose();
				selectedItem = combo.getSelectedIndex();
				rec.audioInputChosen(inputMixers[selectedItem]);
			}
		});
		submitMIDI.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				audioWin.dispose();
				try {
					rec.MIDIinputChosen();
				} catch (MidiUnavailableException e1) {
					e1.printStackTrace();
				} catch (InvalidMidiDataException e1) {
					e1.printStackTrace();
				}
			}
		});
		column.add(combo);
		column.add(submit);
		column.add(submitMIDI);
		JPanel settings = new JPanel();
		settings.add(column);
		settings.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		this.getContentPane().add(settings);
		this.pack();
		this.setVisible(true);
	}
	
	public int getMixer()
	{
		return selectedItem;
	}
}